﻿using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.ComponentModel.Composition.Registration;
using System.Data.Entity;
using System.Reflection;
using System.Web.Http;
using Seed.Data;
using Seed.IoC;

namespace Seed
{
    public class Global : MefHttpApplication
    {

        protected override void OnApplicationStarted()
        {
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            Database.SetInitializer(new DatabaseInitializer());
        }

        protected override ComposablePartCatalog CreateCatalog()
        {
            var rb = new RegistrationBuilder();

            rb.ForTypesDerivedFrom<IRepository>()
              .Export<IRepository>()
              .SetCreationPolicy(CreationPolicy.NonShared);

            rb.ForTypesDerivedFrom<ApiController>()
              .Export()
              .SetCreationPolicy(CreationPolicy.NonShared);

            return new AssemblyCatalog(Assembly.GetExecutingAssembly(), rb);
        }
    }
}